package com.zjsd.sqeybusiness.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.HyinfoMapper;
import com.zjsd.sqeybusiness.domain.Hyinfo;
import com.zjsd.sqeybusiness.service.IHyinfoService;

/**
 * 会员信息Service业务层处理
 * 
 * @author Zheng
 * @date 2025-03-06
 */
@Service
public class HyinfoServiceImpl implements IHyinfoService 
{
    @Autowired
    private HyinfoMapper hyinfoMapper;
    @Autowired
    private SysOperLogMapper sysOperLogManager;

    /**
     * 查询会员信息
     * 
     * @param hyid 会员信息ID
     * @return 会员信息
     */
    @Override
    public Hyinfo selectHyinfoById(Long hyid)
    {
        return hyinfoMapper.selectHyinfoById(hyid);
    }

    /**
     * 查询会员信息列表
     * 
     * @param hyinfo 会员信息
     * @return 会员信息
     */
    @Override
    public List<Hyinfo> selectHyinfoList(Hyinfo hyinfo)
    {
        return hyinfoMapper.selectHyinfoList(hyinfo);
    }

    /**
     * 新增会员信息
     * 
     * @param hyinfo 会员信息
     * @return 结果
     */
    @Override
    public int insertHyinfo(Hyinfo hyinfo)
    {
        return hyinfoMapper.insertHyinfo(hyinfo);
    }

    /**
     * 修改会员信息
     * 
     * @param hyinfo 会员信息
     * @return 结果
     */
    @Override
    public int updateHyinfo(Hyinfo hyinfo)
    {
        return hyinfoMapper.updateHyinfo(hyinfo);
    }

    /**
     * 批量删除会员信息
     * 
     * @param hyids 需要删除的会员信息ID
     * @return 结果
     */
    @Override
    public int deleteHyinfoByIds(Long[] hyids)
    {
        return hyinfoMapper.deleteHyinfoByIds(hyids);
    }

    /**
     * 删除会员信息信息
     * 
     * @param hyid 会员信息ID
     * @return 结果
     */
    @Override
    public int deleteHyinfoById(Long hyid)
    {
        return hyinfoMapper.deleteHyinfoById(hyid);
    }

    @Override
    public Hyinfo insertHyinfoByOpenid(Hyinfo hyinfo) {
        Hyinfo hyinfo1 = new Hyinfo();
        hyinfo1.setOpenid(hyinfo.getOpenid());
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setTitle("保存会员信息");
        sysOperLog.setOperName("保存会员信息");
        sysOperLog.setOperParam("{ openid:" + hyinfo.getOpenid()+ "}");
        sysOperLog.setOperTime(new Date());
        sysOperLogManager.insertOperlog(sysOperLog);
        List<Hyinfo> hyinfos = hyinfoMapper.selectHyinfoListByOpenid(hyinfo1);
        if(hyinfos != null && hyinfos.size() > 0){
            return hyinfos.get(0);
        }else{
            hyinfo.setRzbz("0");
            hyinfo.setCzsj(new Date());
            hyinfo.setHydjname("普通会员");
            int i= hyinfoMapper.insertHyinfo(hyinfo);
            return  hyinfo ;
        }
    }

    @Override
    public Hyinfo selectHyinfoByOpenid(String openid) {
        Hyinfo hyinfo1 = new Hyinfo();
        hyinfo1.setOpenid(openid);
        List<Hyinfo> hyinfos = hyinfoMapper.selectHyinfoListByOpenid(hyinfo1);
        if(hyinfos != null && hyinfos.size()>0){
            return hyinfos.get(0);
        }else{
            return null;
        }
    }
}
